﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using MongoDB;

namespace SkynetMongoDBAccess.Converter
{
    /// <summary>
    /// 类型转换类
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-04-18
    /// </remarks>
    internal static class DocumentToEntityConverter
    {
        public static GridFileEntity ConvertToGridFileEntity(this Document source)
        {
            GridFileEntity entity = new GridFileEntity();

            if (source["_id"] != null)
                entity.id = source["_id"].ToString().Trim();

            if (source["filename"] != null)
                entity.filename = source["filename"].ToString();

            if (source["chunkSize"] != null)
                entity.chunkSize = source["chunkSize"].ToString();

            if (source["contentType"] != null)
                entity.contentType = source["contentType"].ToString();

            if (source["uploadDate"] != null)
                entity.uploadDate = DateTime.Parse(source["uploadDate"].ToString());

            if (source["length"] != null)
                entity.length = source["length"].ToString();

            if (source["md5"] != null)
                entity.md5 = source["md5"].ToString();
            return entity;
        }

        public static UserEntity ConvertToUserEntity(this Document source)
        {
            UserEntity entity = new UserEntity();
            if (source["user"] != null)
                entity.User = source["user"].ToString().Trim();
            if (source["pwd"] != null)
                entity.Pwd = source["pwd"].ToString().Trim();
            if (source["readOnly"] != null)
                entity.IsReadOnly = bool.Parse(source["readOnly"].ToString().Trim());
            return entity;
        }

        //public static IndexesEntity ConvertToIndexesEntity(this Document source)
        //{
        //    IndexesEntity entity = new IndexesEntity();

        //    if (source["name"] != null)
        //        entity.Name = source["name"].ToString().Trim();
        //    if (source["ns"] != null)
        //    {
        //        string[] split =source["ns"].ToString().Split('.');
        //        entity.DataBaseName = split[0].Trim();
        //        entity.CollectionName = split[1].Trim();
        //    }

        //    return entity;
        //}

        public static DataSet ConvertToDataSet(this IEnumerable<Document> source)
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            if (source.Count() > 0)
            {
                foreach (string str in source.First().Keys)
                {
                    table.Columns.Add(str);
                }
                foreach (Document doc in source)
                {
                    DataRow row = table.NewRow();
                    foreach (DataColumn column in table.Columns)
                    {
                        row[column.ColumnName] = doc[column.ColumnName];
                    }
                    table.Rows.Add(row);
                }
            }
            ds.Tables.Add(table);
            return ds;
        }
    }
}
